const { McpServer, ResourceTemplate } = require("@modelcontextprotocol/sdk/server/mcp.js");
const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
const { transformChinese } = require("./utils/intl.js");
const { z } = require("zod");

// Create an MCP server
const server = new McpServer({
  name: "caculator-server",
  version: "1.0.0"
});

// Add an addition tool
server.tool(
  "add100",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: 'caculate result: ' + String(a + b + 100) }]
  })
);

// Simple tool with parameters
server.tool(
  "calculate-bmi",
  {
    weightKg: z.number(),
    heightM: z.number()
  },
  async ({ weightKg, heightM }) => ({
    content: [{
      type: "text",
      text: `BMI = ${(weightKg / (heightM * heightM)).toFixed(2)}`
    }]
  })
);
// 为国际化，提取中文字符串
server.tool(
  "extract-chinese-characters",
  {
    input: z.string(),
    fileName: z.string()
  },
  async ({ input, fileName }) => {
    const matches = transformChinese(input, fileName);
    return {
      content: [{
        type: "text",
        text: JSON.stringify(matches)
      }]
    };
  }
);

// Add a dynamic greeting resource
server.resource(
  "greeting",
  new ResourceTemplate("greeting://{name}", { list: undefined }),
  {
    title: "Greeting Resource",      // Display name for UI
    description: "Dynamic greeting generator"
  },
  async (uri, { name }) => ({
    contents: [{
      uri: uri.href,
      text: `Hello, ${name}!`
    }]
  })
);

// 创建一个模拟面试的对话开始。
server.prompt(
  "interview",
  { industry: z.string(), role: z.string() },
  async ({ industry, role }) => ({
    content: [{ type: "text", text: `我们将进行一个模拟面试。你是${industry}行业的面试官，我是应聘${role}职位的候选人。` }]
  })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
server.connect(transport).then(() => {
  console.log("Server is running");
});